package edu.caltech.android;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.android.c2dm.C2DMBaseReceiver;

import edu.caltech.android.c2dm.DeviceRegistrar;

/**
 * This class was envisioned to receive C2DM messages, process them, 
 * and then launch task-specific intents. Might be better for each activity or service 
 * to have their own private class extending C2DMBaseReceiver?
 * 
 * Maybe the intent should carry a ProtocolBuffer? 
 * 
 * I envision that different message types should be conveyed via C2DM. 
 * How should I structure my receiver to respond to different message types?
 * This seems to reduce to, how do I convey different message types via Intents?
 * Normally, the intent would  be constructed with a different "action string" for each 
 * message type, and the extras would be used to pass message parameters.
 * 
 *   See http://code.google.com/android/c2dm/index.html#received_data
 *  
 *  For now, assume C2DM is only used for alert, and we'll just agree on the intent extras.
 *  
 * @author mfaulk
 *
 */
public class C2DMReceiver extends C2DMBaseReceiver {
	private static final String TAG = "C2DMReceiver";

	public static final String GOT_C2DM = "edu.caltech.android.GOT_C2DM";

	public static final String C2DM_ALERT_ID = "C2DM_alert_id";
	public static final String C2DM_EVENT_TIME = "C2DM_event_time";
	public static final String C2DM_LATITUDE = "C2DM_latitude";
	public static final String C2DM_LONGITUDE = "C2DM_longitude";
	public static final String C2DM_MAGNITUDE = "C2DM_magnitude";
	public static final String C2DM_CONFIDENCE = "C2DM_confidence";	
	
	public C2DMReceiver() {
		super(DeviceRegistrar.SENDER_ID);
	}

	@Override
	public void onRegistered(Context context, String registrationId)
			throws java.io.IOException {
		//Log.d(TAG, "Registration ID arrived: " + registrationId);
		DeviceRegistrar.registerWithServer(context, registrationId);
	};

	@Override
	protected void onMessage(Context context, Intent intent) {
		Log.d(TAG, "onMessage");
		
		
		String alertIDStr = intent.getExtras().getString(C2DM_ALERT_ID);
		String eventTimeStr = intent.getExtras().getString(C2DM_EVENT_TIME);
		String latitudeStr = intent.getExtras().getString(C2DM_LATITUDE); 
		String longitudeStr = intent.getExtras().getString(C2DM_LONGITUDE); 
		String magnitudeStr = intent.getExtras().getString(C2DM_MAGNITUDE); 
		String confidenceStr = intent.getExtras().getString(C2DM_CONFIDENCE);
		
		if (alertIDStr != null) {
			Log.d(TAG,alertIDStr);
		}
			
		if (eventTimeStr != null) {
			Log.d(TAG,eventTimeStr);
		}
		
		if (latitudeStr != null) {
			Log.d(TAG,latitudeStr);
		}

		if (longitudeStr != null) {
			Log.d(TAG,longitudeStr);
		}

		if (magnitudeStr != null) {
			Log.d(TAG,magnitudeStr);
		}

		if (confidenceStr != null) {
			Log.d(TAG,confidenceStr);
		}
		
		Bundle extras = intent.getExtras();

		Intent gotMessageIntent = new Intent(GOT_C2DM);
		//gotMessageIntent.putExtra("Message", "message");
		if(extras != null) {
			gotMessageIntent.putExtras(extras);
		}
		context.sendBroadcast(gotMessageIntent);
	}

	@Override
	public void onError(Context context, String errorId) {
		Log.e(TAG, "Error occurred! " + errorId);
	}

}
